package com.rykj.energy.task.api.rest;

import com.rykj.energy.common.dto.CameraDto;
import com.rykj.energy.common.dto.TaskCameraRelationDto;
import com.rykj.energy.common.utils.Result;
import com.rykj.energy.task.api.service.ITaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "任务管理模块-基于摄像头维度")
@Slf4j
@RestController
@RequestMapping("/task/camera")
public class CameraTaskController {

    @Autowired
    private ITaskService taskService;

    @ApiOperation("单个摄像头启动识别任务-用于单个摄像头启动任务")
    @PostMapping("/startTasksForCamera/{cameraId}")
    public Result startTasks(@PathVariable("cameraId") int cameraId,@RequestBody List<TaskCameraRelationDto> taskInfos){
        try {
            taskService.saveBatchTaskForCamera(cameraId,taskInfos);
        }catch (Exception e){
            log.error("启动识别任务异常===",e);
           return Result.failure(e.getMessage());
        }
        return Result.success("保存成功",null);
    }

    @ApiOperation("批量启动多摄像头识别任务-用于多个摄像头同时勾选任务")
    @PostMapping("/startBatchTasksForCameras")
    public Result startBatchTasksForCameras(@RequestBody List<CameraDto> taskInfos){
        try {
            taskService.saveBatchTaskForCamera(taskInfos);
        }catch (Exception e){
            log.error("启动识别任务异常===",e);
            return Result.failure(e.getMessage());
        }
        return Result.success("保存成功",null);
    }

    @ApiOperation("根据摄像头id获取任务信息")
    @GetMapping("/getTaskCameraByCameraId/{cameraId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tasksStatus", value = "任务状态:run执行中,wait待执行", paramType = "query"),
    })
    public Result<List<TaskCameraRelationDto>> getTaskCameraByCameraId(@PathVariable("cameraId") String cameraId,
                                                                       @RequestParam(value = "tasksStatus",required = false) String tasksStatus){
        try {
            List<TaskCameraRelationDto> taskCameras= taskService.getTaskCameraByCameraId(cameraId,tasksStatus);
            return Result.success(taskCameras);
        }catch (Exception e){
            log.error("===根据摄像头id获取任务信息异常===",e);
            return Result.failure("根据摄像头id获取任务信息异常");
        }
    }
}
